package com.fenghuajueli.module_user.activity.register;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;

import androidx.lifecycle.Observer;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.blankj.utilcode.util.LogUtils;
import com.fenghuajueli.libbasecoreui.constants.AppConfigInfo;
import com.fenghuajueli.lib_data.entity.UserInfoEntity;
import com.fenghuajueli.module_route.UserModuleRoute;
import com.fenghuajueli.module_user.R;
import com.fenghuajueli.module_user.activity.UserViewModelBaseActivity;
import com.fenghuajueli.module_user.databinding.ActivityRegisterBinding;
import com.fenghuajueli.libbasecoreui.viewmodel.BaseViewModel;
import com.fenghuajueli.module_user.model.UserViewModel;


@Route(path = UserModuleRoute.USER_REGISTER)
public class RegisterActivity extends UserViewModelBaseActivity implements View.OnClickListener {
    private boolean phoneIsInputComplete = false;
    private boolean passwordIsInputComplete = false;
    private boolean phoneIsConfirmInputComplete = false;
    private boolean passwordIsConfirmInputComplete = false;

    ActivityRegisterBinding binding;
    private final UserViewModel userViewModel = new UserViewModel();

    @Override
    public BaseViewModel setBaseViewModel() {
        return userViewModel;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityRegisterBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        setTextChangeListener();
        binding.btnRegister.setOnClickListener(this);
        binding.ivCloseRegister.setOnClickListener(this);
        userViewModel.registerSuccess.observe(this, new Observer<UserInfoEntity>() {
            @Override
            public void onChanged(UserInfoEntity userInfoEntity) {
                LogUtils.d("注册成功了--------------》");
            }
        });
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.btnRegister) {
            register();
        } else if (view.getId() == R.id.ivCloseRegister) {
            finish();
        }
    }

    private void changeButtonStatus() {
        if (passwordIsInputComplete && phoneIsInputComplete && phoneIsConfirmInputComplete && passwordIsConfirmInputComplete) {
            binding.btnRegister.setBackgroundResource(R.drawable.shape_btn_can_use_bg);
            binding.btnRegister.setEnabled(true);
        } else {
            binding.btnRegister.setBackgroundResource(R.drawable.shape_btn_not_use_bg);
            binding.btnRegister.setEnabled(false);
        }
    }

    private void register() {
        if (userViewModel != null) {
            userViewModel.register(this, binding.etInputPhone.getText().toString().trim(),
                    binding.etInputPassword.getText().toString().trim(),
                    binding.etConfirmInputPhone.getText().toString().trim(),
                    binding.etConfirmInputPassword.getText().toString().trim());
        }
    }

    private void setTextChangeListener() {
        binding.etInputPhone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (TextUtils.isEmpty(s.toString().trim())) {
                    phoneIsInputComplete = false;
                } else {
                    phoneIsInputComplete = true;
                }
                changeButtonStatus();
            }
        });
        binding.etInputPassword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (TextUtils.isEmpty(s.toString().trim())) {
                    passwordIsInputComplete = false;
                } else {
                    passwordIsInputComplete = true;
                }
                changeButtonStatus();
            }
        });
        binding.etConfirmInputPhone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (TextUtils.isEmpty(s.toString().trim())) {
                    phoneIsConfirmInputComplete = false;
                } else {
                    phoneIsConfirmInputComplete = true;
                }
                changeButtonStatus();
            }
        });
        binding.etConfirmInputPassword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (TextUtils.isEmpty(s.toString().trim())) {
                    passwordIsConfirmInputComplete = false;
                } else {
                    passwordIsConfirmInputComplete = true;
                }
                changeButtonStatus();
            }
        });
    }
}